package controller;

import model.Goods;
import model.Manager;
import model.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import service.IOrderService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @aythor:侯仪澳
 * @data:2022/7/21
 */
@Controller
@RequestMapping("order")
public class OrderController {
    @Autowired
    private IOrderService orderService;

    //查询所有的订单信息
    @RequestMapping("queryAllOrder")
    public ModelAndView queryAllOrder(Order order,Integer pageNo) {
        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> map = orderService.orderLimitPage(order, pageNo);
        List<Order> list = orderService.queryAllOrder(order);
        //将前端传来的查询信息返回给前端，用于保留值
        modelAndView.addObject("order", order);
        //将后端进行的分页数据  传给前端
        modelAndView.addObject("list",  map.get("list"));
        modelAndView.addObject("pageNo", map.get("pageNo"));
        modelAndView.addObject("totalPage", map.get("totalPage"));
        modelAndView.setViewName("manager/order_list");
        return modelAndView;
    }

    //查询所有的退款中的所有订单信息
    @RequestMapping("queryAllRefund")
    public ModelAndView queryAllRefund(Order order,Integer pageNo) {
        order.setOrderStatus(5);//5- 退款中的状态值
        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> map = orderService.orderLimitPage(order, pageNo);
        List<Order> list = orderService.queryAllOrder(order);
        modelAndView.addObject("list",  map.get("list"));
        modelAndView.addObject("pageNo", map.get("pageNo"));
        modelAndView.addObject("totalPage", map.get("totalPage"));
        modelAndView.setViewName("manager/order_refund");
        return modelAndView;
    }

    //一键发送货物
    @RequestMapping("sendGoods")
    @ResponseBody
    public String sendGoods(@RequestParam(value = "ids[]") List<String> list){
        List<Order> list1 = new ArrayList<>();
        //将前端传入的集合 根据orderNo进行查询   将查询出来的所有值取出放入实体类中，再放入新的集合中
        for (int i = 0;i < list.size();i++){
            List<Order> orders = orderService.queryAllOrder(new Order(list.get(i)));
            list1.add(orders.get(0));
        }
        return orderService.updateSendOrderBatch(list1);
    }

    //一键退款货物
    @RequestMapping("refundGoods")
    @ResponseBody
    public String refundGoods(@RequestParam(value = "ids[]") List<String> list){
        List<Order> list1 = new ArrayList<>();
        //将前端传入的集合取出放入实体类中（再加入状态值），再放入新的集合中
        for (int i = 0;i < list.size();i++){
            list1.add(new Order(list.get(i),5));
        }
        return orderService.updateRefundOrderBatch(list1);
    }
}
